package edu.gatech.ealf.ettplaf;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.Timer;

import edu.gatech.ealf.EasyAccessibilityLookAndFeel;
import edu.gatech.ealf.GlassPaneManager;

public class ETTHelper implements MouseListener, ActionListener {
    private static ETTHelper instance = null;
    
    private ToolTipComponent toolTipComponent;
    //private JScrollPane toolTipScrollPane = null;
    private Container lastGlass = null;
    private Dimension toolTipSize = new Dimension(100, 20);
    private boolean mouseIn = false;
    private Timer t = null;
    private ETTHelper(){
        toolTipComponent = new ToolTipComponent();
        toolTipComponent.setVisible(false);

        //toolTipScrollPane = new JScrollPane(toolTipComponent);
        //toolTipScrollPane.setVisible(false);
        toolTipComponent.setSize(toolTipSize);
        toolTipComponent.setPreferredSize(toolTipSize);
        toolTipComponent.setMinimumSize(toolTipSize);

        //toolTipScrollPane.setSize(toolTipSize);
        //toolTipScrollPane.setPreferredSize(toolTipSize);
        //toolTipScrollPane.setMinimumSize(toolTipSize);
        
        //toolTipScrollPane.addMouseListener(this);
        toolTipComponent.addMouseListener(this);
        //toolTipScrollPane.getHorizontalScrollBar().addMouseListener(this);
        /*
        Component[] barParts = toolTipScrollPane.getHorizontalScrollBar().getComponents();
        for(Component c : barParts){
        	if(c != null){
        		c.addMouseListener(this);
        	}
        }
        
        toolTipScrollPane.getVerticalScrollBar().addMouseListener(this);
        barParts = toolTipScrollPane.getVerticalScrollBar().getComponents();
        for(Component c : barParts){
        	if(c != null){
        		c.addMouseListener(this);
        	}
        }
        */
    }
    
    public static ETTHelper getInstance(){
        if(instance == null){
            instance = new ETTHelper();
        }
        return instance;
    }
    
    public void setToolTipText(Container glass, String toolTipText, JComponent comp){
        if(lastGlass != null){
            removeToolTip();
        }
        toolTipComponent.setText(toolTipText);
        toolTipComponent.setForeground(ExtendedToolTipLookAndFeel.foregroundColor);
        toolTipComponent.setBackground(ExtendedToolTipLookAndFeel.backgroundColor);
        toolTipComponent.setVisible(true);
        //toolTipScrollPane.setVisible(true);
        glass = (Container) comp.getRootPane().getGlassPane();

        lastGlass = glass;
        glass.setLayout(null);
        glass.add(toolTipComponent);
        glass.setComponentZOrder(toolTipComponent, 0);
        //toolTipScrollPane.repaint();
        toolTipComponent.repaint();
        setPosition(glass, comp);
        GlassPaneManager.show(glass, toolTipComponent);
        
    }

    private void removeToolTip() {
    	if(lastGlass != null && toolTipComponent != null){
	        lastGlass.remove(toolTipComponent);
	        GlassPaneManager.hide(lastGlass, toolTipComponent);
    	}
    	lastGlass = null;
    }
    
    private void setPosition(Container glass, JComponent comp){
        Point componentPos = EasyAccessibilityLookAndFeel.getAbsolutePosition(comp);
        Dimension glassSize = glass.getSize();
        Dimension compSize = comp.getSize();
        Dimension actualToolTipSize = toolTipComponent.getSize();
        
        int x = componentPos.x;
        int y = componentPos.y + compSize.height;
        boolean xOK = false;
        boolean yOK = false;
        //System.out.println(y + ", " + actualToolTipSize.height + ", " + (y + actualToolTipSize.height));
        if((x + actualToolTipSize.width) < glassSize.width){
            xOK = true;
        }
        if((y + actualToolTipSize.height) < glassSize.height){
            yOK = true;
        }
        if(xOK && yOK){
            toolTipComponent.setBounds(x, y, toolTipSize.width, toolTipSize.height);
            return;
        }
        if(!xOK){
            x = x - actualToolTipSize.width;
        }
        if(!yOK){
            y = componentPos.y - actualToolTipSize.height;
            //System.out.println("Y Not OK");
            //System.out.println(y);
        }
        toolTipComponent.setBounds(x, y, toolTipSize.width, toolTipSize.height);
        return;
        
    }
    
    
    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    	mouseIn = true;
    }

    public void mouseExited(MouseEvent e) {
    	mouseIn = false;
    	
    	requestRemoveToolTip();
    }
    
    public void requestRemoveToolTip(){
    	if(t != null){
    		t.stop();
    	}
    	t = new Timer(500, this);
        t.start();
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

	public void actionPerformed(ActionEvent arg0) {
		t.stop();
		if(!mouseIn){
			removeToolTip();
		}
	}

}
